package org.pure.ioc;

import org.pure.ioc.annotation.Bean;
import org.pure.ioc.scanner.Scanner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by will.lin on 2015/8/10.
 */
public class BeanFactory {

    public static  Map<Class<?>,Object> beanMap=new HashMap<Class<?>, Object>();

    private static List<Class<?>> classList= Scanner.classList;

    static{
        if(classList!=null&&classList.size()!=0){
            System.out.println(classList.size());
            for(Class<?> clazz:classList){
                if(clazz.isAnnotationPresent(Bean.class)){
                    try {
                        Object value=clazz.newInstance();
                        beanMap.put(clazz,value);
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }


    public static <T> T getBean(Class<T> key){
         return  (T)beanMap.get(key);
    }

    public static void setBean(Class<?> clazz,Object val){
        beanMap.put(clazz,val);
    }





}
